/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gride.plugin.javadoc;

import gride.gui.GUIManager;
import gride.plugin.GridePlugin;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

/**
 *
 * @author lukasz
 */
public class JavadocBrowserPlugin extends GridePlugin
{
    /**
     * Location of the Java API
     */
    public static String JAVADOC_INDEX =
                                "http://java.sun.com/javase/6/docs/api/";
    /**
     * Location of the Groovy API
     */
    public static String GROOVYDOC_INDEX =
                                "http://groovy.codehaus.org/gapi/";

    /**
     * Location of the Groovy JDK
     */
    public static String GROOVY_JDK_INDEX =
                                "http://groovy.codehaus.org/groovy-jdk/";

    // <editor-fold defaultstate="expanded" desc="Private Members">

    private JTabbedPane tabbedPane;
    private JEditorPane javaPane;
    private JEditorPane groovyPane;
    private JEditorPane groovyJDKPane;

    private JCheckBoxMenuItem showDocMenuItem;

    // </editor-fold>


    // <editor-fold defaultstate="expanded" desc="Constructors">

    /**
     * Default constructor
     */
    public JavadocBrowserPlugin()
    {
        tabbedPane = new JTabbedPane();

        groovyJDKPane = createDocBrowser();
        tabbedPane.add("Groovy JDK", new JScrollPane(groovyJDKPane));

        groovyPane = createDocBrowser();
        tabbedPane.add("Groovy API", new JScrollPane(groovyPane));
        
        javaPane = createDocBrowser();
        tabbedPane.add("Java API", new JScrollPane(javaPane));

        showDocMenuItem = new JCheckBoxMenuItem("Show API Documentation");
        showDocMenuItem.setSelected(true);
        showDocMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (showDocMenuItem.isSelected())
                    guiManager.addComponent(GUIManager.MAIN_PANE, tabbedPane,
                                            "API Documentation Browser");
                else
                    guiManager.removeComponent(tabbedPane);
            }
        });
    }

    // </editor-fold>


    // <editor-fold defaultstate="expanded" desc="Getters/Setters">

    // </editor-fold>


    // <editor-fold defaultstate="expanded" desc="Public Methods">

    /**
     * Activates the plugin
     */
    @Override
    public void activate() 
    {
        guiManager.addComponent(GUIManager.MAIN_PANE, tabbedPane,
                                "API Documentation");
        guiManager.focusOnComponent(tabbedPane);

        guiManager.addMenuItem(showDocMenuItem, "Tools");


        try
        {
            javaPane.setPage(JAVADOC_INDEX);
            groovyPane.setPage(GROOVYDOC_INDEX);
            groovyJDKPane.setPage(GROOVY_JDK_INDEX);
        }
        catch (IOException e)
        {

        }
        
        active = true;
    }

    /**
     * Deactivates the plugin
     */
    @Override
    public void deactivate() 
    {
        guiManager.removeComponent(tabbedPane);
        guiManager.removeMenuItem(showDocMenuItem);

        active = false;
    }

    // </editor-fold>


    // <editor-fold defaultstate="expanded" desc="Private Methods">

    private JEditorPane createDocBrowser()
    {
        final JEditorPane pane = new JEditorPane();
        pane.setEditorKit(new HTMLEditorKit()
        {
            @Override
            public Document createDefaultDocument()
            {
                Document doc = super.createDefaultDocument();
                ((HTMLDocument)doc).setAsynchronousLoadPriority(-1);
                return doc;
            }
        });
        pane.setEditable(false);
        pane.setText("Loading...");
        
        pane.addHyperlinkListener(new HyperlinkListener()
        {
            public void hyperlinkUpdate(HyperlinkEvent e)
            {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                {
                    // check if this link should be handled by the
                    // page itself (frames)
                    if (e instanceof HTMLFrameHyperlinkEvent)
                    {
                        HTMLDocument doc = (HTMLDocument)pane.getDocument();
                        doc.processHTMLFrameHyperlinkEvent(
                                                (HTMLFrameHyperlinkEvent)e);
                    }
                    else
                        try
                        {
                            pane.setPage(e.getURL());
                        }
                        catch (IOException ex)
                        {
                            return;
                        }
                }
            }
        });
        
        return pane;
    }

    // </editor-fold>

}
